// pages/team/team.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command

Page({

  /**
   * 页面的初始数据
   */
  data: {
    top_label: [
      {
        name: '添加',
        color: 'lightgreen'
      },
      {
        name: '删除',
        color: 'lightgreen'
      },
      {
        name: '修改',
        color: 'lightgreen'
      },
    ],
    stafflist: [],
    choicenumber: 0,
    choicevalue: 0,
    valuearray: [],
    p_choice: 0,
  },

  //三选一
  sanxuanyi(e) {
    //setData给动态下标数组的写法
    var index = "top_label[" + e.currentTarget.dataset.number + "].color"
    this.setData({
      'top_label[0].color': 'lightgreen',
      'top_label[1].color': 'lightgreen',
      'top_label[2].color': 'lightgreen',
      [index]: 'grey',
      p_choice: e.currentTarget.dataset.number
    })
  },

  //勾选
  checkboxChange: function (e) {
    this.data.choicenumber = e.detail.value.length
    this.data.choicevalue = e.detail.value
    this.data.valuearray = e.detail.value
    console.log(e.detail.value)
  },

  //确认
  confirm() {
    var that = this
    //确认操作者是否拥有管理员权限
    console.log(app.globalData.mobile)
    db.collection('staff')
      .where({
        mobile: app.globalData.mobile
      })
      .get()
      .then(res => {
        console.log(res.data[0])
        if (res.data[0].station == '管理员') {
          //修改
          if (that.data.p_choice == 2) {
            if (that.data.choicenumber == 1) {
              wx.navigateTo({
                url: '/pages/tjxg/tjxg?mobile=' + that.data.choicevalue,
              })
            } else {
              wx.showModal({
                title: '提醒',
                content: '信息修改只能单选！',
                showCancel: false
              })
            }
          }
          //添加
          else if (that.data.p_choice == 0) {
            wx.navigateTo({
              url: '/pages/tjxg/tjxg',
            })
          }
          //删除
          else if (that.data.p_choice == 1) {
            db.collection('staff')
              .where({
                mobile: _.in(that.data.valuearray)
              })
              .remove({
                success(res) {
                  console.log(res)
                  wx.showModal({
                    title: '提醒',
                    content: '删除已完成！',
                    showCancel: false
                  })
                  wx.redirectTo({
                    url: '/pages/team/team',
                  })
                }
              })
          }
        }else{
          wx.showModal({
            title: '提醒',
            content: '您没有相应权限！',
            showCancel: false
          })
        }
      })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    //获取本社区管理成员列表
    db.collection('staff')
      .where({
        block: wx.getStorageSync('block') //app.globalData.block
      })
      .get({
        success: function (res) {
          that.setData({
            stafflist: res.data
          })
          console.log(res)
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})